package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Product;
import cn.wolfcode.trip.base.query.ProductQuery;
import cn.wolfcode.trip.base.service.IProductService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


@Controller
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private IProductService productService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ProductQuery qo) {
        PageInfo pageInfo = productService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "product/list";
    }

    /*@RequestMapping("/getContentById")
    @ResponseBody
    public StrategyContent getContentById(Long id) {
        StrategyContent content = productService.getContentById(id);
        return content;
    }*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Product product, MultipartFile file) {
        JsonResult result = new JsonResult();
        try {
            //如果上传了背景图片就设置背景图片
            if (file != null && file.getSize() > 0) {
                String imgUrl = UploadUtil.uploadQiniu(file);
                product.setImgUrl(imgUrl);
            }
            productService.saveOrUpdate(product);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
